package com.haidao.admin.component.web.controller;

import cn.hutool.extra.spring.SpringUtil;
import com.haidao.admin.component.entity.ReportOrgInfo;
import com.haidao.admin.component.service.ReportService;
import com.haidao.admin.core.exception.Assert;
import com.haidao.admin.core.result.Resp;
import com.opencsv.bean.CsvToBean;
import com.opencsv.bean.CsvToBeanBuilder;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.util.List;

/**
 * @author lyy
 * @Date 2023/11/11
 */
@Slf4j
@RestController
@RequestMapping("/file")
public class FileUploadController {

    @PostMapping("/upload")
    public Resp<String> upload(@RequestParam("file") MultipartFile multipartFile,String type) throws IOException {
        String filename = multipartFile.getOriginalFilename();
        Assert.isTrue(StringUtils.isNotEmpty(filename), "上传文件不存在");
        InputStream inputStream = multipartFile.getInputStream();
        InputStreamReader reader=new InputStreamReader(inputStream, Charset.forName("UTF-8"));
        ReportService bean = SpringUtil.getBean(type, ReportService.class);
        bean.save(reader);
        return Resp.ok();
    }
}
